import React from 'react';
import {observer} from "mobx-react";
import {Bubble, Modal, Tools} from "weaver-mobile";
import * as authTypes from "../../constants/secondAuthType";
import DynamicPassword from "./DynamicPassword";
import DynamicToken from "./DynamicToken";
import SecondAuthPassword from "./SecondAuthPassword";
import CAAuth from "./CAAuth";
import {doSecondAuth} from "../../apis/secondAuth";
import "./style/index.less";

const getLabel = Tools.getLabel;

@observer
export default class Main extends React.Component {
    constructor(props) {
        super(props);
    }

    componentWillReceiveProps(nextProps, nextContext) {
        const visible1 = this.props.visible;
        const visible2 = nextProps.visible;

        if (visible1 != visible2 && !visible2) {        //关闭窗口
            const {secondAuthStore} = this.props;
            secondAuthStore.saveAuthCode("");       //将设置的密码清空
        }
    }

    render() {
        const {visible, secondAuthStore} = this.props;
        const {secondAuthType} = secondAuthStore;

        const title = getLabel(501195, "身份验证");
        const footer = [
            {text: getLabel(501196, "确定"), onPress: this.doSubmit.bind(this)},
            {text: getLabel(31129, "取消"), onPress: this.closeDialog.bind(this)},
        ];

        if (secondAuthType == authTypes.CA_AUTH) {      //CA认证
            const {onFail, onSuccess, onClose} = this.props;
            const props = {onFail, onSuccess, onClose};
            return <CAAuth secondAuthStore={secondAuthStore} visible={visible} {...props}/>
        }else {
            return (
                <Modal
                    visible={visible}
                    transparent={true}
                    maskClosable={false}
                    onClose={this.closeDialog.bind(this)}
                    title={title}
                    footer={footer}
                    className="sencond-auth-dialog"
                    wrapClassName="second-auth-test"
                >
                    {this.renderContent()}
                </Modal>
            );
        }
    }

    renderContent() {
        const {visible, secondAuthStore} = this.props;
        const authKey = secondAuthStore.authKey;
        const secondAuthType = secondAuthStore.secondAuthType;

        let content = <div></div>;
        switch (secondAuthType) {
            case authTypes.DYNAMIC_PASSWORD:
                content = <DynamicPassword needSend={visible} secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.DYNAMIC_TOKEN:
                content = <DynamicToken secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.SECOND_AUTH_PASSWORD:
                content = <SecondAuthPassword secondAuthStore={secondAuthStore}/>;
                break;
            case authTypes.QYS:
                break;
            default:
                break;
        }

        return content;
    }

    //提交
    doSubmit() {
        const {secondAuthStore} = this.props;
        const {authCode, secondAuthType, isProtectDialog, authKey} = secondAuthStore;

        if (!authCode) {
            let errorMsg = "";

            switch (secondAuthType) {
                case authTypes.DYNAMIC_PASSWORD:
                    errorMsg = getLabel(501199, "请输入动态密码");
                    break;
                case authTypes.DYNAMIC_TOKEN:
                    errorMsg = getLabel(501200, "请输入动态令牌");
                    break;
                case authTypes.SECOND_AUTH_PASSWORD:
                    errorMsg = getLabel(501201, "请输入二次验证密码");
                    break;
                default:
                    break;
            }

            if (!!errorMsg) {
                Bubble.error(errorMsg, 1);
            }
        } else {
            const params = {authCode, authType: secondAuthType};
            doSecondAuth(params).then(data => {     //验证二次授权码
                const {api_status, api_errormsg} = data;
                if (api_status) {
                    // this.closeDialog();

                    if (this.props.onSuccess) {
                        this.props.onSuccess.call({}, {isProtectDialog});
                    }
                } else {
                    if (!!api_errormsg) {
                        Bubble.error(api_errormsg, 1);
                    }

                    if (this.props.onFail) {
                        this.props.onFail.call({}, {...data, secondAuthType});
                    }
                }
            })
        }
    }

    //关闭
    closeDialog() {
        if (this.props.onClose) {
            this.props.onClose.call({});
        }
    }
}
